<template>
  <el-form ref="form" :model="teacherInfo" :rules="rules" label-width="100px">
    <el-form-item label="工号" prop="teacherNo">
      <el-input v-model="teacherInfo.teacherNo" placeholder="请输入工号" maxlength="50" />
    </el-form-item>
    
    <el-form-item label="职称" prop="title">
      <el-select v-model="teacherInfo.title" placeholder="请选择职称" style="width: 100%">
        <el-option label="教授" value="教授"></el-option>
        <el-option label="副教授" value="副教授"></el-option>
        <el-option label="讲师" value="讲师"></el-option>
        <el-option label="助教" value="助教"></el-option>
        <el-option label="其他" value="其他"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="研究方向" prop="researchDirection">
      <el-input
        v-model="teacherInfo.researchDirection"
        type="textarea"
        :rows="3"
        placeholder="多个方向用逗号分隔，如：机器学习,数据挖掘,自然语言处理"
        maxlength="500"
        show-word-limit
      />
    </el-form-item>

    <el-form-item label="最大指导数" prop="maxStudents">
      <el-input-number
        v-model="teacherInfo.maxStudents"
        :min="1"
        :max="30"
        controls-position="right"
        style="width: 200px"
      />
      <span style="margin-left: 10px; color: #909399; font-size: 13px">
        可指导的最大学生数量
      </span>
    </el-form-item>

    <el-form-item label="联系邮箱" prop="email">
      <el-input v-model="teacherInfo.email" placeholder="请输入联系邮箱" maxlength="100" />
    </el-form-item>

    <el-form-item label="办公室位置" prop="officeLocation">
      <el-input v-model="teacherInfo.officeLocation" placeholder="如：教学楼A-301" maxlength="200" />
    </el-form-item>

    <el-form-item label="个人简介" prop="introduction">
      <el-input
        v-model="teacherInfo.introduction"
        type="textarea"
        :rows="5"
        placeholder="请介绍您的教学和科研情况、学术成果、研究兴趣等"
        maxlength="2000"
        show-word-limit
      />
    </el-form-item>

    <el-form-item label="备注">
      <el-input
        v-model="teacherInfo.remark"
        type="textarea"
        :rows="2"
        placeholder="其他补充信息"
        maxlength="500"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" size="medium" @click="submit">保存</el-button>
      <el-button type="default" size="medium" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getTeacherProfile, saveOrUpdateTeacher } from "@/api/system/user";

export default {
  name: "TeacherInfo",
  data() {
    return {
      // 教师信息
      teacherInfo: {
        teacherNo: '',
        title: '',
        researchDirection: '',
        maxStudents: 8,
        email: '',
        officeLocation: '',
        introduction: '',
        remark: ''
      },
      // 表单校验规则
      rules: {
        teacherNo: [
          { required: true, message: "请输入工号", trigger: "blur" }
        ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },
  created() {
    this.getTeacherInfo();
  },
  methods: {
    /** 获取教师信息 */
    getTeacherInfo() {
      getTeacherProfile().then(response => {
        if (response.data) {
          this.teacherInfo = response.data;
        }
      }).catch(() => {
        // 如果没有教师信息，保持默认值
      });
    },
    /** 提交按钮 */
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          saveOrUpdateTeacher(this.teacherInfo).then(response => {
            this.$modal.msgSuccess("保存成功");
            // 重新获取数据
            this.getTeacherInfo();
          });
        }
      });
    },
    /** 关闭按钮 */
    close() {
      const obj = { path: "/index" };
      this.$tab.closeOpenPage(obj);
    }
  }
};
</script>